package com.cruzr.opengles.utils;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class FileUtils {

    private static final String TAG = "FileUtils";

    public static void unzipFile(String directory, String srcPath) {
        Log.i(TAG,"unzipFile -> srcPath : " + srcPath);
        try {
            InputStream inputStream = new FileInputStream(directory + File.separator + srcPath);
            ZipInputStream zipInputStream = new ZipInputStream(inputStream);
            ZipEntry zipEntry = zipInputStream.getNextEntry();
            byte[] buffer = new byte[1024 * 1024];
            int count;
            while (zipEntry != null) {
                if (!zipEntry.isDirectory()) {
                    String fileName = zipEntry.getName();
                    fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1);
                    File file = new File(directory + File.separator + fileName);
                    file.createNewFile();
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    while ((count = zipInputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, count);
                    }
                    fileOutputStream.close();
                }
                zipEntry = zipInputStream.getNextEntry();
            }
            zipInputStream.close();
            Log.i(TAG, "unzipFile success");
        } catch (Exception e) {
            Log.e(TAG, "unzipFile Exception", e);
        }
    }
}
